/**
 * Created with JetBrains WebStorm.
 * User: basil
 * Date: 13-8-19
 * Time: 下午6:17
 * To change this template use File | Settings | File Templates.
 */
KISSY.add(function (_kissy) {
    var _event = _kissy.Event,
        _dom = _kissy.DOM,
        _anim = _kissy.Anim,
        _document=document,
        _window=window,
        _auto_image_url="/Home/SelectImageSetting";

    function hasImageSetting(set, folder) {
        var image_setting = null;
        _kissy.each(set, function (elem, i) {
            if (elem.folder == folder) {
                image_setting = elem;
                return false;
            }
        });
        return image_setting;
    }
    function checkViewPortWidth() {
        var _width = _dom.viewportWidth();
        if (_width >= 1200) {
            return "wide";
        } else if (_width >= 768 && _width <= 979) {
            return "pad";
        } else if (_width < 768) {
            return "mobile";
        } else {
            return "default";
        }
    }
    var _default_options = {
        thumbnailsBannerEL:"Banner_simple",
        featureCarouselEL:"featureCarousel"
    };

    function AutoImage(_opts) {
        var _options = _kissy.merge(_default_options, _opts || {});
        if (!(this instanceof AutoImage)) {
            return new AutoImage(_options);
        }
        this._init(_options);
    }
    _kissy.augment(AutoImage,{
        configList: null,
        thumbnailsBannerEL:null,
        featureCarouselEL:null,
        changeImagePath:function(alias,folder)
        {
            //2013-12-20 basilwang todo need kissy future
            $('div[data-image-setting=' + alias + '] img').each(function (i, img) {
                var $img = $(img);

                var $slide_li = $img.parent();
                var source = $img.data("source");
                if (source) {
                    var data = "Upload/" + folder + "/" + source;
                    if (data) {
                        $img.attr("data-src", data);
                        $slide_li.attr("data-bottom-thumb", data);
                    }
                }
                $img.unveil();


            });
        },
        thumbnailsBanner:function(_width,_height,_screen_num){
            var _auto_image=this;
            if (_auto_image.thumbnailsBannerEL !== null) {
                _auto_image.thumbnailsBannerEL.allinone_thumbnailsBanner({
                    skin: 'simple',
                    numberOfThumbsPerScreen: _screen_num,
                    width: _width,
                    height: _height,
                    thumbsReflection: 0,
                    defaultEffect: 'random'
                });
            }
        },
        featureCarousel:function(_width,_height){
            var _auto_image=this;
            if (_auto_image.featureCarouselEL !== null) {
                _auto_image.featureCarouselEL.featureCarousel({
                    largeFeatureWidth: _width,
                    largeFeatureHeight: _height,
                    smallFeatureWidth: _width / 2,
                    smallFeatureHeight: _height / 2
                });
            }

        },
        iterate: function () {
            var _auto_image=this;
            var _config_list=_auto_image.configList;
            _kissy.each(_config_list, function (item, i) {
                var images = item.ImageSettings;
                var _device_type = checkViewPortWidth();
                var _number_of_thumbs_per_screen = 0,
                    _auto_image_width = 0,
                    _auto_image_height = 0,
                    _acc_min_width,
                    _screen_num = 0;
                switch (_device_type) {
                    case "wide":
                        _acc_min_width = "115px";
                        _screen_num = 7;
                        break;
                    case "pad":
                        _acc_min_width = "30px";
                        _screen_num = 5;
                        break;
                    case "mobile":
                        _acc_min_width = "20px";
                        _screen_num = 3;
                        break;
                    default:
                        _acc_min_width = "70px";
                        _screen_num = 7;
                }
                if (images !== null && images.length !== 0) {
                    var image_setting = hasImageSetting(images, _device_type);
                    if (image_setting === null) {
                        image_setting = hasImageSetting(images, "default");
                    }
                    folder = _device_type;
                    _auto_image_width = image_setting.width;
                    _auto_image_height = image_setting.height;

                    _auto_image.changeImagePath(item.alias, folder);

                    if (item.alias == "Slide") {
                        _auto_image.thumbnailsBanner(_auto_image_width, _auto_image_height, _screen_num);
                    }
                    if (item.alias == "Feature") {
                        _auto_image.featureCarousel(_auto_image_width, _auto_image_height);
                    }
                }

            });

        },
        _init: function (_options) {
            var _auto_image=this;
            _kissy.io({
                type: "get",
                url: _auto_image_url,
                dataType: "json",
                success: function (_config_list) {
                    if (!_kissy.isArray(_config_list) || !_config_list.length)
                        return;
                    _auto_image.configList = _config_list;
                    //2013-12-20 basilwang todo use kissy future
                    _auto_image.featureCarouselEL = $("#" + _options.featureCarouselEL);
                    _auto_image.thumbnailsBannerEL = $("#" + _options.thumbnailsBannerEL);
                    _auto_image.iterate();
                }
            });
        }
    });
    return {
        init: function (_options) {
            new AutoImage(_options)
        }
    }
}, {  requires: []
});